package controller

import (
	"douyin/models"
	"douyin/repository/mysql"
	"douyin/service"
	"douyin/utils/pack"
	"strconv"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// RelationAction 关系操作，关注或取关
func RelationAction(c *gin.Context) {
	//1.获取querystring参数和参数校验
	p := new(models.RelationRequest)
	if err := c.ShouldBind(&p); err != nil {
		// 请求参数有误，直接返回响应
		zap.L().Error("controller.RelationAction with invalid param", zap.Error(err))
		pack.ResponseError(c, models.CodeInvalidParam)
		return
	}
	//2. 业务处理
	userId, _ := c.Get(pack.CtxUserIDKey)
	err := service.RelationAction(p.ToUserId, p.ActionType, userId.(int64))
	if err != nil {
		zap.L().Error("service.RelationAction failed", zap.Error(err))
		pack.ResponseError(c, models.CodeRelationActionError)
		return
	}
	//3.返回响应
	pack.ResponseSuccess(c, models.CodeSuccess)
}

// FollowList 用户关注列表
func FollowList(c *gin.Context) {
	userId, _ := c.Get(pack.CtxUserIDKey)                      //登录用户id
	otherId, _ := strconv.ParseInt(c.Query("user_id"), 10, 64) //对方用户id

	//调用业务处理
	followList, err := service.FollowList(userId.(int64), otherId)
	//查询空处理
	if err == mysql.ErrorQueryZeroRecord {
		zap.L().Error("service.FollowList ErrorQueryZeroRecord!! ", zap.Error(err))
		models.ResponseRelationSuccess(c, nil)
		return
	}
	if err != nil {
		zap.L().Error("service.FollowList failed", zap.Error(err))
		models.ResponseRelationError(c, models.CodeGetFollowListError)
		return
	}
	//返回响应
	models.ResponseRelationSuccess(c, followList)
}

// FollowerList 用户粉丝列表
func FollowerList(c *gin.Context) {
	userId, _ := c.Get(pack.CtxUserIDKey)                      //登录用户id
	otherId, _ := strconv.ParseInt(c.Query("user_id"), 10, 64) //对方用户id
	//调用业务处理
	followerList, err := service.FollowerList(userId.(int64), otherId)
	//查询空处理
	if err == mysql.ErrorQueryZeroRecord {
		zap.L().Error("service.FollowerList ErrorQueryZeroRecord!! ", zap.Error(err))
		models.ResponseRelationSuccess(c, nil)
		return
	}
	if err != nil {
		zap.L().Error("service.FollowerList failed", zap.Error(err))
		models.ResponseRelationError(c, models.CodeGetFollowerListError)
		return
	}
	//返回响应
	models.ResponseRelationSuccess(c, followerList)
}
